package com.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.common.SetMeta;
import com.shop.common.level.RightLevel;
import com.shop.entity.Right;
import com.shop.entity.Role;
import com.shop.exception.RightException;
import com.shop.mapper.RightsMapper;
import com.shop.mapper.RoleMapper;
import com.shop.service.RightsService;
import com.shop.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("rightsService")
public class RightsServiceImpl implements RightsService {

    @Autowired
    private RightsMapper rightsMapper;

    @Autowired
    private RoleMapper roleMapper;

    private List<Right> rightList;

    /**
     * 所有权限列表
     * @param type
     * @return
     * @throws RightException
     */
    @Override
    public SetMeta selectRights(String type) throws RightException {
        if(!type.equals("list") && !type.equals("tree")) throw new RightException("参数错误");
        SetMeta setMeta = new SetMeta();
        rightList = rightsMapper.selectList(null);

        if (type.equals("list")) {
            setMeta.setSuccess(rightList, "获取权限列表成功");
        } else {
            List<Right> levelOne = getRightList(RightLevel.LEVEL_ONE.level);
            List<Right> levelTwo = getRightList(RightLevel.LEVEL_TWO.level);
            merge(levelOne, levelTwo);
            List<Right> levelThree = getRightList(RightLevel.LEVEL_THREE.level);
            merge(levelTwo, levelThree);

            setMeta.setSuccess(levelOne,"获取权限列表成功");
        }
        return setMeta;
    }

    /**
     * 左侧菜单权限
     * @return
     */
    @Override
    public SetMeta selectLeftRights(Integer rid) {
        SetMeta setMeta = new SetMeta();

        if (rid != 0) {
            Role role = roleMapper.selectById(rid);
            if (role == null) {
                setMeta.setError("该用户的角色不存在，请重新登录");
                return setMeta;
            } else {
                String[] psIds = role.getPsIds().split(",");
                rightList = rightsMapper.selectRightLists(psIds);
            }

        } else {
            rightList = rightsMapper.selectRightList();
        }


        List<Right> level_one = getLeftRightsList(RightLevel.LEVEL_ONE.level);
        List<Right> level_two = getLeftRightsList(RightLevel.LEVEL_TWO.level);
        List<Right> level_three = getLeftRightsList(RightLevel.LEVEL_THREE.level);
        mergeLeft(level_one,level_two);

        setMeta.setSuccess(level_one,"获取菜单列表成功");

        return setMeta;
    }

    private List<Right> getRightList(String type) {
        List<Right> list = new ArrayList<>();
        for (Right right : this.rightList) {
            if (type.equals(right.getLevel())) {
                right.setLevel(null);
                list.add(right);
            }
        }
        return list;
    }

    private void merge(List<Right> parents, List<Right> childrens) {
        for (Right parent : parents) {
            List<Right> child = new ArrayList<>();
            for (Right children : childrens) {
                if (children.getPid().equals(parent.getId())) {
                    child.add(children);
                }
            }
            parent.setChildren(child);
        }
    }

    private List<Right> getLeftRightsList(String type) {
        List<Right> list = new ArrayList<>();
        for (Right right : this.rightList) {
            if (type.equals(right.getLevel())) {
                right.setLevel(null);
                right.setChildren(new ArrayList<>());
                list.add(right);
            }
        }
        return list;
    }

    private void mergeLeft(List<Right> parents, List<Right> childrens) {
        for (Right parent : parents) {
            List<Right> child = new ArrayList<>();
            for (Right children : childrens) {
                if (parent.getId().equals(children.getPid())) {
                    children.setPid(null);
                    child.add(children);
                }
            }
            parent.setPid(null);
            parent.setChildren(child);
        }
    }

}
